package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.common.exception.BusinessException;
import com.mask.im.business.mapper.AnnouncementMapper;
import com.mask.im.business.model.dto.AnnouncementDTO;
import com.mask.im.business.model.dto.MailDTO;
import com.mask.im.business.model.entity.AnnouncementDO;
import com.mask.im.business.model.vo.AnnouncementVO;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.AnnouncementService;
import com.mask.im.business.common.util.BeanCopy;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import com.mask.im.business.common.util.PageUtil;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

import static com.mask.im.business.common.constant.CommonConstant.TRUE;


/**
 * @author jian
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, AnnouncementDO> implements AnnouncementService {

	@Override
	public RestResponse<List<AnnouncementDO>> getAnnouncement() {
		List<AnnouncementDO> announcement = this.baseMapper.getLastAnnouncement();
		return RestResponse.ok(announcement);
	}

	@SneakyThrows
	@Override
	public PageResult<AnnouncementDTO> draftAnnouncement(String title, Integer status) {
		CompletableFuture<Integer> draftAnnouncementCount = CompletableFuture.supplyAsync(() ->
				this.baseMapper.draftAnnouncementCount(title, status));
		List<AnnouncementDTO> announcementDTOList = this.baseMapper.draftAnnouncement(title,status,
				PageUtil.getCurrentPage(),PageUtil.getSize());
		return PageResult.<AnnouncementDTO>builder()
				.data(announcementDTOList)
				.total(draftAnnouncementCount.get()).build();
	}

	@Override
	public RestResponse<String> saveAnnouncement(AnnouncementVO announcementVO) {
        AnnouncementDO announcement = BeanCopy.singleCopy(announcementVO, AnnouncementDO.class);
		announcement.setPublisher(MaskIMSecurityUtils.getUserId());
		int flag;
		if (Objects.isNull(announcementVO.getId())){
			//TODO 判断是否为即时公告，是，则通channel发送到在线的用户
			flag = this.baseMapper.insert(announcement);
		}else {
			flag = this.baseMapper.updateById(announcement);
		}
		if (flag>0){
			return RestResponse.ok();
		}
		return RestResponse.fail();
	}

	@SneakyThrows
	@Override
	public PageResult<AnnouncementDTO> publishAnnouncement(String publishDate, String draftTitle) {
		CompletableFuture<Integer> count = CompletableFuture.supplyAsync(() ->
				this.baseMapper.publishAnnouncementCount(publishDate, draftTitle));
		List<AnnouncementDTO> announcementDTOList = this.baseMapper.publishAnnouncement(publishDate, draftTitle,PageUtil.getCurrentPage(),PageUtil.getSize());
		return PageResult.<AnnouncementDTO>builder().data(announcementDTOList).total(count.get()).build();
	}

	@Override
	public RestResponse<String> deleteAnnouncement(Integer id) {
        AnnouncementDO announcement = this.baseMapper.selectOne(new LambdaQueryWrapper<AnnouncementDO>()
				.eq(AnnouncementDO::getId,id).eq(AnnouncementDO::getIsDelete,0));
		if (Objects.isNull(announcement)){
			throw new BusinessException("公告不存在或已删除");
		}
		announcement.setIsDelete(TRUE);
		int update = this.baseMapper.updateById(announcement);
		if (update>0){
			return RestResponse.ok("删除成功");
		}
		return RestResponse.fail("删除失败");
	}

	@Override
	public RestResponse<MailDTO> announcementById(Integer id) {
        AnnouncementDO announcement = this.baseMapper.selectById(id);
		if (announcement==null){
			throw new BusinessException("公告不存在");
		}
		MailDTO mailDTO = new MailDTO();
		mailDTO.setContent(announcement.getContent());
		mailDTO.setTitle(announcement.getTitle());
		mailDTO.setCreateTime(announcement.getCreateTime());
		return RestResponse.ok(mailDTO);
	}
}

